#include "freertos.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"

#include "led.h"
//#include "malloc.h"
//#include "variable.h"

//任务优先级
#define TASK_LED_TASK_PRIO		(configMAX_PRIORITIES-1)
//任务堆栈大小
#define TASK_LED_STK_SIZE 		96
//任务堆栈
StackType_t task_led_stack[TASK_LED_STK_SIZE];
//任务控制块
StaticTask_t task_led_tcb;
//任务句柄
TaskHandle_t task_led_handler;
//任务函数

//extern SemaphoreHandle_t xSem_treat_start[3];
extern EventGroupHandle_t  xEventGroupHandle;
static void task_led(void *pvParameters)
{
//	uint32_t time_total[3] = 0;
//	uint8_t tmp[3] = 0;
	//EventBits_t startflag;
    //UBaseType_t uxHighWaterMark;
    //uxHighWaterMark = uxTaskGetStackHighWaterMark( NULL );

	while (1)
	{
		vTaskDelay(1000);
		GPIO_Toggle(GPIO1, 3);
		//i++;
		//_set_uint32(10,i);
#if 0
		for (uint8_t i = 0;3>i;i++)
		{
			if (xEventGroupGetBits(xEventGroupHandle) & (1 << i))
			{				
				(_var_uint8(TREAT_ANIMATION1 + i))++;
				if(_var_uint8(TREAT_ANIMATION1 + i) > 6)
				{
					_var_uint8(TREAT_ANIMATION1 + i) = 1;
				}

				if(_var_uint32(TIME_REMAIN_1 + 4*i) > 0)
				{
					(_var_uint32(TIME_REMAIN_1 + 4*i))--;
				}
				else
				{
					xEventGroupClearBits(xEventGroupHandle,(1 << i));
				}
			}
			else
			{
				
				_var_uint8(TREAT_ANIMATION1 + i) = 0;
			}
		}		
#endif
		//uint16_t percent1, percent2;
		//percent1 = my_mem_perused(SRAMIN);
		//percent2 = my_mem_perused(SRAMEX);
		//printf("sram used:%d,sdram used:%d\n",percent1,percent2);
		//my_mem_perused();		
	}
}


void task_led_init(void)
{
#if 0
	task_led_handler = xTaskCreateStatic((TaskFunction_t)task_led,
										 (const char *)"task_led",
										 (uint32_t)TASK_LED_STK_SIZE,
										 (void *)NULL,
										 (UBaseType_t)TASK_LED_TASK_PRIO,
										 (StackType_t *)task_led_stack,
										 (StaticTask_t *)&task_led_tcb);
#else
    xTaskCreate(task_led, "task_led", 96, NULL, 12, NULL);
#endif
}
